import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { Role } from '@/types'

export const useRoleStore = defineStore('role', () => {
  const roles = ref<Role[]>([
    // 系统角色
    {
      id: 1,
      name: '系统管理员',
      code: 'admin',
      description: '拥有系统所有权限',
      type: 'system',
      permissions: ['*'],
      createTime: '2024-01-01 10:00:00',
      updateTime: '2024-01-01 10:00:00'
    },
    {
      id: 2,
      name: '管理员',
      code: 'manager',
      description: '拥有管理权限',
      type: 'system',
      permissions: ['user:read', 'user:write', 'role:read', 'department:read', 'contractor:read'],
      createTime: '2024-01-01 10:00:00',
      updateTime: '2024-01-01 10:00:00'
    },
    {
      id: 3,
      name: '普通用户',
      code: 'user',
      description: '普通用户权限',
      type: 'system',
      permissions: ['jsa:read', 'jsa:write'],
      createTime: '2024-01-01 10:00:00',
      updateTime: '2024-01-01 10:00:00'
    },
    
    // 业务角色 - 权威人员
    {
      id: 4,
      name: '2级权威人员',
      code: 'authority_level_2',
      description: '2级作业权威人员，负责2级作业的审核',
      type: 'business',
      level: 2,
      permissions: ['jsa:review_level_2', 'permit:approve_level_2'],
      createTime: '2024-01-01 10:00:00',
      updateTime: '2024-01-01 10:00:00'
    },
    {
      id: 5,
      name: '3级权威人员',
      code: 'authority_level_3',
      description: '3级作业权威人员，负责3级作业的审核',
      type: 'business',
      level: 3,
      permissions: ['jsa:review_level_3', 'permit:approve_level_3'],
      createTime: '2024-01-01 10:00:00',
      updateTime: '2024-01-01 10:00:00'
    },
    
    // 业务角色 - 批准人
    {
      id: 6,
      name: '2级批准人',
      code: 'approver_level_2',
      description: '2级作业批准人，负责2级作业的批准',
      type: 'business',
      level: 2,
      permissions: ['jsa:approve_level_2', 'permit:approve_level_2'],
      createTime: '2024-01-01 10:00:00',
      updateTime: '2024-01-01 10:00:00'
    },
    {
      id: 7,
      name: '3级批准人',
      code: 'approver_level_3',
      description: '3级作业批准人，负责3级作业的批准',
      type: 'business',
      level: 3,
      permissions: ['jsa:approve_level_3', 'permit:approve_level_3'],
      createTime: '2024-01-01 10:00:00',
      updateTime: '2024-01-01 10:00:00'
    },
    
    // 业务角色 - 区域负责人
    {
      id: 8,
      name: '区域负责人',
      code: 'area_manager',
      description: '负责特定区域的作业管理',
      type: 'business',
      permissions: ['area:manage', 'jsa:review', 'permit:review'],
      createTime: '2024-01-01 10:00:00',
      updateTime: '2024-01-01 10:00:00'
    },
    
    // 业务角色 - 安全监督
    {
      id: 9,
      name: '安全监督',
      code: 'safety_supervisor',
      description: '负责安全监督和检查',
      type: 'business',
      permissions: ['safety:supervise', 'jsa:review', 'permit:review', 'safety:inspect'],
      createTime: '2024-01-01 10:00:00',
      updateTime: '2024-01-01 10:00:00'
    },
    
    // 其他业务角色
    {
      id: 10,
      name: '作业负责人',
      code: 'operation_manager',
      description: '负责具体作业的执行',
      type: 'business',
      permissions: ['operation:execute', 'jsa:create', 'permit:apply'],
      createTime: '2024-01-01 10:00:00',
      updateTime: '2024-01-01 10:00:00'
    },
    {
      id: 11,
      name: '技术专家',
      code: 'technical_expert',
      description: '提供技术支持和指导',
      type: 'business',
      permissions: ['technical:advise', 'jsa:review', 'permit:review'],
      createTime: '2024-01-01 10:00:00',
      updateTime: '2024-01-01 10:00:00'
    }
  ])

  // 获取所有角色
  const getAllRoles = () => {
    return Promise.resolve(roles.value)
  }

  // 根据ID获取角色
  const getRoleById = (id: number) => {
    const role = roles.value.find(r => r.id === id)
    return Promise.resolve(role)
  }

  // 根据类型获取角色
  const getRolesByType = (type: 'system' | 'business') => {
    const filtered = roles.value.filter(r => r.type === type)
    return Promise.resolve(filtered)
  }

  // 根据等级获取角色
  const getRolesByLevel = (level: number) => {
    const filtered = roles.value.filter(r => r.level === level)
    return Promise.resolve(filtered)
  }

  // 获取权威人员角色
  const getAuthorityRoles = () => {
    const filtered = roles.value.filter(r => r.code.includes('authority'))
    return Promise.resolve(filtered)
  }

  // 获取批准人角色
  const getApproverRoles = () => {
    const filtered = roles.value.filter(r => r.code.includes('approver'))
    return Promise.resolve(filtered)
  }

  // 创建角色
  const createRole = (role: Omit<Role, 'id' | 'createTime' | 'updateTime'>) => {
    const newRole: Role = {
      id: Math.max(...roles.value.map(r => r.id)) + 1,
      ...role,
      createTime: new Date().toLocaleString(),
      updateTime: new Date().toLocaleString()
    }
    roles.value.push(newRole)
    return Promise.resolve(newRole)
  }

  // 更新角色
  const updateRole = (id: number, role: Partial<Role>) => {
    const index = roles.value.findIndex(r => r.id === id)
    if (index !== -1) {
      roles.value[index] = {
        ...roles.value[index],
        ...role,
        updateTime: new Date().toLocaleString()
      }
      return Promise.resolve(roles.value[index])
    }
    return Promise.reject(new Error('角色不存在'))
  }

  // 删除角色
  const deleteRole = (id: number) => {
    const index = roles.value.findIndex(r => r.id === id)
    if (index !== -1) {
      roles.value.splice(index, 1)
      return Promise.resolve(true)
    }
    return Promise.reject(new Error('角色不存在'))
  }

  return {
    roles,
    getAllRoles,
    getRoleById,
    getRolesByType,
    getRolesByLevel,
    getAuthorityRoles,
    getApproverRoles,
    createRole,
    updateRole,
    deleteRole
  }
})
